package com.example.auth3.config;

import com.example.auth3.util.DfUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
import org.springframework.security.oauth2.provider.approval.TokenApprovalStore;
import org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler;
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * 安全登录认证 配置 继承类
 * @author shangSshang
 * 2018.12.21 12:00:03
 */
@Slf4j
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebsecuityImp extends WebSecurityConfigurerAdapter {
  private   static final  Logger logger = LoggerFactory.getLogger(WebsecuityImp.class);

    /**
     * security 登录安全认证 配置 在此处
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.
       /* http
                // .requestMatchers().antMatchers 需要忽略认证
                .requestMatchers().antMatchers("/login","/index")
                .and()
                // authorizeRequests().anyRequest().permitAll() 其他的需要校验token
                .authorizeRequests()
                .anyRequest()
//                .antMatchers("/login","/index")
                .permitAll()//校验token
//                .anyRequest().
//                authenticated()
//                .antMatchers("/*","/**")
//                .permitAll()
                .and().formLogin()
                //默认登录跳转
                .loginPage("/login")
                //成功后 处理器
                .successHandler(loginSuccessHandler)
                //失败后处理器
                .failureHandler(loginFailHandler);*/



        http.authorizeRequests()
                //放开登录拦截的地址
                .antMatchers( DfUtils.arrayList(whiteList,resourceList)).permitAll() // 放开权限的url
                .anyRequest().authenticated().and()
                //是否需要httpBasic 基本认证
                .httpBasic()
                .and()
                //是否关闭 csrf(跨域站点请求) 保护
                .csrf().disable()
//                .formLogin()
                //默认登录跳转
//                .loginPage("/login")
//                .successForwardUrl("/index")
                //成功后 处理器
//                .successHandler(loginSuccessHandler)
                //失败后处理器
//                .failureHandler(loginFailHandler);
        ;
    }

    /**
     * 静态配置信息引入
     */
    @Value("#{'${myapp.whiteList}'.split(',')}")
    private String[] whiteList;
    @Value("#{'${myapp.resourceList}'.split(',')}")
    private String[] resourceList;



    @Autowired
    public UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     *用户登录信息进行验证
     * 实现了查询用户信息的接口
     * 重写了密码管理基类
     * @param auth
     * @throws Exception
     */
    @Autowired
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }

    /**
     * 重写了父方法 如果不变动 可以不修改
     * @return
     * @throws Exception
     */
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


}
